package com.centaur0.tags;

import java.io.File;
import java.io.IOException;
import java.util.List;

import javax.servlet.ServletContext;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.SimpleTagSupport;

import com.centaur0.tags.util.JspContextWrapper;
import com.centaur0.ui.menu.EstruturaMenu;
import com.centaur0.ui.menu.GeneralMenuStyle;
import com.centaur0.ui.menu.Menu;
import com.centaur0.ui.menu.XmlMenuConfigParser;
import com.centaur0.util.Application;

public class RodapeTag extends SimpleTagSupport {
	public String path;
	public String div_id;
	public String separador;
	
	public String getSeparador() {
		return separador;
	}

	public void setSeparador(String separador) {
		this.separador = separador;
	}

	public String getDiv_id() {
		return div_id;
	}

	public void setDiv_id(String div_id) {
		this.div_id = div_id;
	}

	public String getPath() {
		return path;
	}

	public void setPath(String path) {
		this.path = path;
	}

	@Override
	public void doTag() throws JspException, IOException {
		ServletContext svlCtx = Application.getHttpApplicationContext();
		File file = new File(svlCtx.getRealPath(path));
		EstruturaMenu em = null;
		try{
			em = XmlMenuConfigParser.parseDoc(file);
		} catch(Exception e){
			throw new JspException(e);
		}
		this.printHtml(em);
	}
	
	private void printHtml(EstruturaMenu em) throws JspException, IOException{
		StringBuilder out = new StringBuilder();
		GeneralMenuStyle gms = em.getGeneralMenuStyle();
		boolean flagInnerCellStyle;
		boolean flagLabelStyle;
		
		if((flagInnerCellStyle = (gms.getInnerCellStyleId() != null && !gms.getInnerCellStyleId().isEmpty())) |
				(flagLabelStyle = (gms.getLabelStyleId() != null && !gms.getLabelStyleId().isEmpty()))){
			out.append("<style type='text/css'>\n");
				if(flagInnerCellStyle){
					out.append("\n".concat(gms.getInnerCellStyle()));
				}
				if(flagLabelStyle){
					out.append("\n".concat(gms.getLabelStyle()));
				}
			out.append("</style>");
		}
		List<Menu> menus = em.getMenus();
		out.append(String.format("<div id='%s'>\n\t", div_id == null? this.toString().substring(this.toString().indexOf("@")+1): div_id));
		out.append("<table style='border:0px;'>\n\t\t");
				out.append("<tr>\n\t\t");
				StringBuilder buffer = new StringBuilder("<style type='text/css'>\n");
				int len = menus.size();
				int i = 0;
				for(Menu menu: menus){
					String labelStyleId = "_NENHUM_LABELSTYLE_RODAPE".concat(menu.toString().substring(menu.toString().indexOf("@")+1));
					String labelStyleClass = "";
					if(menu.getLabelStyleId() != null && !menu.getLabelStyleId().isEmpty()){
						labelStyleClass = menu.getLabelStyleId();
						buffer.append("\n".concat(menu.getLabelStyle()));
					} else if(flagLabelStyle){
						labelStyleClass = gms.getLabelStyleId();
					}
					
					String innerCellStyleId = "_NENHUM_INNERCELLSTYLE_RODAPE".concat(menu.toString().substring(menu.toString().indexOf("@")+1));
					String innerCellStyleClass = "";
					if(menu.getInnerCellStyleId() != null && !menu.getInnerCellStyleId().isEmpty()){
						innerCellStyleClass  = menu.getInnerCellStyleId();
						buffer.append("\n".concat(menu.getInnerCellStyle()));
					} else if(flagInnerCellStyle){
						innerCellStyleClass = gms.getInnerCellStyleId();
					}
					
					out.append("<td style='border:0px;width:auto'>\n\t\t\t");
						out.append(String.format("<div id='%s' class='%s'>\n\t\t\t\t", innerCellStyleId, innerCellStyleClass));
							if(menu.getLink() != null && menu.getLink().isAjax()){
								AjaxLinkTag tag = new AjaxLinkTag();
								JspContextWrapper wrapper = new JspContextWrapper(getJspContext());
								tag.setJspContext(wrapper);
								tag.setParent(this);
								tag.setAguarde(menu.getLink().getAguarde());
								tag.setDescription(menu.getLabel());
								tag.setFundoEscuro(menu.getLink().getFundoEscuro());
								tag.setHrefId(labelStyleId);
								tag.setHrefClass(labelStyleClass);
								tag.setOuterId(menu.getLink().getOuterId());
								tag.setPath(menu.getLink().getText());
								tag.doTag();
								out.append(wrapper.getOutString());
							} else {
								out.append(String.format("<a id='%s' class='%s' href='%s'", labelStyleId, labelStyleClass, (menu.getLink() == null || menu.getLink().getText().isEmpty()? 
										"javascript:;":menu.getLink().getText())).concat(">\n\t\t\t\t\t"));
									out.append("&nbsp;".concat(menu.getLabel()).concat("&nbsp;\n\t\t\t\t"));
								out.append("</a>\n\t\t\t");
							}						
						out.append("</div>\n\t\t");
					out.append("</td>\n\t\t");
					if(++i < len && (separador != null)){
						out.append("<td style='border:0px;width:auto'>\n\t\t\t");
							out.append("<div>\n\t\t\t\t");
								out.append("&nbsp;".concat(separador).concat("&nbsp;\n\t\t\t\t"));							
							out.append("</div>\n\t\t");
						out.append("</td>\n\t\t");
					}
				}
				buffer.append("\n</style>");
				out.append("</tr>\n\t");
			out.append("</table>\n");
		out.append("</div>\n");
		this.getJspContext().getOut().append(buffer.toString());
		this.getJspContext().getOut().append(out.toString());
	}
}
